<?php
/*! \page license
 *
 * Copyright (c) 2011, Blaise Lengrand
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this list
 * of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this
 * list of conditions and the following disclaimer in the documentation and/or
 * other materials provided with the distribution.
 * Neither the name of `snWin' nor the names of its contributors may be used to
 * endorse or promote products derived from this software without specific prior
 * written permission.
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

  global $_CURRENT;
  global $_PAGE_PARAMS;

  // Initialize the environement
  require_once("../feed.php");

  // Dependencies
  require_once('system/pages/social/facebook/OAuth.php');
  require_once('system/pages/social/facebook/Facebook.class.php');

  // Application specific
  define("FACEBOOK_APP_ID", $_PAGE_PARAMS['social_facebook_id']);
  define("FACEBOOK_APP_SECRET", $_PAGE_PARAMS['social_facebook_secret']);

  // Generates the callback URL
  $callback_url = $_CURRENT['domain'].$_CURRENT['root']."/system/pages/social/facebook/facebook_connect.php";

  if (isset($_GET['debug']))
  {
    $url = "https://graph.facebook.com/oauth/access_token";
    $result = feed_get($url, array(
      'client_id' => FACEBOOK_APP_ID,
      'client_secret' => FACEBOOK_APP_SECRET,
      'grant_type' => client_credentials
    ));

    if (!$result)
      echo feed_get_error();
    if (!preg_match('/^access_token=(.*)$/', $result, $matches))
      die();
    $app_access_token = $matches[1];
    // Get test user access token
    $url = "https://graph.facebook.com/".FACEBOOK_APP_ID."/accounts/test-users?installed=true&permissions=".FACEBOOK_PERMISSIONS."&access_token=".$app_access_token;
    $result = feed_post($url);
    if (!$result)
      echo feed_get_error();
    echo $result;
    die();
  }

  // New connection
  if (isset($_GET['new']))
  {
    $facebook = new Facebook(FACEBOOK_APP_ID, FACEBOOK_APP_SECRET, $callback_url);
    $facebook->start();
  }
  else
  {
    $facebook = new Facebook(FACEBOOK_APP_ID, FACEBOOK_APP_SECRET, $callback_url);
    $access_token = $facebook->callback();

    echo "<html>";
    echo "<head>";
    echo "<script>";
    echo "function send_token() {";
    echo "try {\n";
    echo "parent.facebook_access_token_set(\"".$access_token."\");\n";
    echo "}catch(e){\n";
    echo "parent.location.href = \"".$_CURRENT['url']."\";\n";
    echo "}";
    echo "}";
    echo "</script>";
    echo "</head>";
    echo "<body onload=\"javascript:send_token();\">";
    echo "</body>";
    echo "</html>";
  }
?>
